﻿using System;
using System.IO;
using System.Xml.Serialization;
namespace CoreModule.Storages
{
    /// <summary>
    /// 数据库配置(当前支持Mysql,MsSql,Oracle)
    /// </summary>
    public class DbStorageConfiguration
    {
        static string XmlFilePath = "DbStorageConfiguration.xml";
        public DbStorageConfiguration()
        {

        }
        /// <summary>
        /// Mysql 优先
        /// </summary>
        /// 
        public string IpAddress { get; set; } = "192.168.18.152";
        /// <summary>
        /// MsSql 次之
        /// </summary>
        public string UserName { get; set; } = "wuhuaqi";
        /// <summary>
        /// Oracle 最后
        /// </summary>
        public string UserPassword { get; set; } = "123456";

        /// <summary>
        /// 
        /// </summary>
        public string Database { get; set; } = "wuhuaqi_db";
        /// <summary>
        /// 
        /// </summary>

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        public static DbStorageConfiguration Load()
        {
            if (File.Exists(XmlFilePath))
            {
                try
                {
                    using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                    {
                        return (DbStorageConfiguration)new XmlSerializer(typeof(DbStorageConfiguration)).Deserialize(fileStream);
                    }
                }
                catch (Exception)
                {
                    File.Delete(XmlFilePath);
                }
            }
            return new DbStorageConfiguration();
        }
        /// <summary>
        /// /
        /// </summary>
        public void Save()
        {
            if (File.Exists(XmlFilePath))
            {
                File.Delete(XmlFilePath);
            }
            try
            {
                using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                {
                    new XmlSerializer(typeof(DbStorageConfiguration)).Serialize(fileStream, this);
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
